<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import OutpatientApi from '@/utils/Interceptors'
import { showToast } from 'vant'

const router = useRouter()
const route = useRoute()
const consultationDetail = ref<any>(null)
const loading = ref(false)
const isLiked = ref(false)
const isCollected = ref(false)
const isDevelopmentMode = ref(false)

// 获取咨询详情
const getConsultationDetail = async (consultId: number) => {
  try {
    loading.value = true
    const response = await OutpatientApi.get('/api/ConsultationCategory/SearchConsultationDetail', {
      params: {
        ConsultId: consultId
      }
    })
    
    if (response.data.code === 200) {
      consultationDetail.value = response.data.data[0] // 取第一个详情
    } else {
      showToast({
        message: response.data.msg || '获取咨询详情失败',
        position: 'middle',
        duration: 2000
      })
    }
  } catch (error) {
    console.error('获取咨询详情失败:', error)
    // 如果API不可用，显示开发模式
    isDevelopmentMode.value = true
    showToast({
      message: '功能开发中，请稍后重试',
      position: 'middle',
      duration: 2000
    })
  } finally {
    loading.value = false
  }
}

// 返回上一页
const goBack = () => {
  router.back()
}

// 关注医生
const followDoctor = async () => {
  showToast({
    message: '关注功能还在开发中，敬请期待！',
    position: 'middle',
    duration: 2000
  })
}

// 点赞
const toggleLike = async () => {
  showToast({
    message: '点赞功能还在开发中，敬请期待！',
    position: 'middle',
    duration: 2000
  })
}

// 收藏
const toggleCollect = async () => {
  showToast({
    message: '收藏功能还在开发中，敬请期待！',
    position: 'middle',
    duration: 2000
  })
}

onMounted(() => {
  const consultId = Number(route.params.consultId)
  if (consultId) {
    getConsultationDetail(consultId)
  }
})
</script>

<template>
  <div class="detail-container">
    <!-- 状态栏 -->
    <div class="status-bar">
      <span class="time">12:30</span>
      <div class="status-icons">
        <span class="signal">📶</span>
        <span class="wifi">📶</span>
        <span class="battery">🔋</span>
      </div>
    </div>

    <!-- 头部 -->
    <div class="header">
      <div class="back-button" @click="goBack">
        <span class="back-icon">←</span>
      </div>
      <h1 class="page-title">资讯详情</h1>
      <div class="header-actions">
        <span class="more-icon">⋯</span>
        <span class="view-icon">👁</span>
      </div>
    </div>

    <!-- 开发模式提示 -->
    <div v-if="isDevelopmentMode" class="development-notice">
      <div class="notice-content">
        <div class="notice-icon">🚧</div>
        <h3 class="notice-title">功能开发中</h3>
        <p class="notice-text">咨询详情功能正在开发中，敬请期待！</p>
        <div class="notice-features">
          <div class="feature-item">
            <span class="feature-icon">📄</span>
            <span>详细咨询内容</span>
          </div>
          <div class="feature-item">
            <span class="feature-icon">👨‍⚕️</span>
            <span>医生专业回复</span>
          </div>
          <div class="feature-item">
            <span class="feature-icon">💬</span>
            <span>互动交流功能</span>
          </div>
        </div>
        <button class="back-home-btn" @click="goBack">返回上一页</button>
      </div>
    </div>

    <!-- 正常功能界面 -->
    <div v-else>
      <!-- 内容区域 -->
      <div class="content-area">
        <div v-if="loading" class="loading">
          <div class="loading-spinner"></div>
          <p>加载中...</p>
        </div>
        
        <div v-else-if="!consultationDetail" class="empty-state">
          <div class="empty-icon">📋</div>
          <p>暂无咨询详情</p>
        </div>
        
        <div v-else class="detail-content">
          <!-- 文章标题 -->
          <div class="article-header">
            <h2 class="article-title">菱角的功效与作用</h2>
            <div class="read-count">
              <span class="eye-icon">👁</span>
              <span>2000人已读</span>
            </div>
          </div>

          <!-- 医生信息 -->
          <div class="doctor-info">
            <div class="doctor-avatar">
              <div class="avatar-placeholder">👨‍⚕️</div>
            </div>
            <div class="doctor-details">
              <div class="doctor-name">{{ consultationDetail.Name || '刘医生' }}</div>
              <div class="doctor-title">{{ consultationDetail.Title || '主任医师' }}</div>
              <div class="doctor-department">{{ consultationDetail.DepartmentId || '门诊外一科' }} 四川省保健院南苑</div>
            </div>
            <button class="follow-btn" @click="followDoctor">关注</button>
          </div>

          <!-- 文章内容 -->
          <div class="article-content">
            <p class="content-text">
              菱角是一年生草本水生植物菱的果实,菱角皮脆肉美,蒸煮后剥壳食用,亦可熬粥食。菱角含有丰富的蛋白质、不饱和脂肪酸及多种维生素和微量元素。具有利尿通乳,止渴,解酒毒的功效。
            </p>
            
            <div class="content-section">
              <h3>1、补脾益气</h3>
              <p>菱角生食有清暑解热作用,熟食则有益气健脾功效。</p>
            </div>
            
            <div class="content-section">
              <h3>2、抗癌</h3>
              <p>菱角还能起到"醒脾、解酒、缓中"的用途。民间单方中还用它治疗癌症。据近代研究资料表明,菱实的醇浸水液有抗癌作用。</p>
            </div>
            
            <div class="content-section">
              <h3>3、减肥</h3>
              <p>菱角利尿、通乳、止消渴、解酒毒,主治疮毒、赘疣,且可健体,是减肥的辅助食品。</p>
            </div>
            
            <div class="content-section">
              <h3>4、缓解皮肤病</h3>
              <p>菱角食用、外用均可,以辅助治疗小儿头疮、头面黄水疮、皮肤赘疣等多种皮肤病。</p>
            </div>
          </div>

          <!-- 医生回复 -->
          <div class="doctor-response">
            <h3 class="response-title">医生回复</h3>
            <div class="response-content">
              <p>{{ consultationDetail.Response || '保证药到病除' }}</p>
              <div class="medical-knowledge">
                <strong>医学知识链接：</strong>
                <p>{{ consultationDetail.MedicalKnowledgeLink || '菱角生食有清暑解热作用，熟食则有益气健脾功效。' }}</p>
              </div>
            </div>
          </div>

          <!-- 标签和发布时间 -->
          <div class="article-footer">
            <div class="article-tags">
              <span class="tag">#中医常识</span>
            </div>
            <div class="publish-time">
              发布于{{ consultationDetail.CreateTime ? new Date(consultationDetail.CreateTime).toLocaleString() : '2020-01-01 15:20:20' }}
            </div>
          </div>
        </div>
      </div>

      <!-- 底部操作栏 -->
      <div class="bottom-actions">
        <button class="comment-btn" disabled>暂无法评论</button>
        <div class="action-buttons">
          <div class="action-item" @click="toggleCollect">
            <span class="action-icon" :class="{ active: isCollected }">❤️</span>
            <span class="action-text">收藏</span>
          </div>
          <div class="action-item" @click="toggleLike">
            <span class="action-icon" :class="{ active: isLiked }">👍</span>
            <span class="action-text">点赞(520)</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.detail-container {
  min-height: 100vh;
  background: #f8f9fa;
  position: relative;
  padding-bottom: 80px;
}

/* 状态栏 */
.status-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 20px;
  background: white;
  font-size: 14px;
  color: #333;
}

.status-icons {
  display: flex;
  gap: 5px;
}

/* 头部 */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 20px;
  background: white;
  border-bottom: 1px solid #eee;
}

.back-button {
  cursor: pointer;
  padding: 5px;
}

.back-icon {
  font-size: 20px;
  color: #333;
}

.page-title {
  font-size: 18px;
  font-weight: bold;
  color: #333;
  margin: 0;
}

.header-actions {
  display: flex;
  gap: 15px;
}

.more-icon, .view-icon {
  font-size: 18px;
  cursor: pointer;
  color: #666;
}

/* 开发模式提示 */
.development-notice {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: calc(100vh - 120px);
  padding: 20px;
}

.notice-content {
  background: white;
  border-radius: 16px;
  padding: 40px 30px;
  text-align: center;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  max-width: 400px;
  width: 100%;
}

.notice-icon {
  font-size: 64px;
  margin-bottom: 20px;
}

.notice-title {
  font-size: 24px;
  font-weight: bold;
  color: #333;
  margin: 0 0 15px 0;
}

.notice-text {
  font-size: 16px;
  color: #666;
  line-height: 1.5;
  margin: 0 0 30px 0;
}

.notice-features {
  display: flex;
  flex-direction: column;
  gap: 15px;
  margin-bottom: 30px;
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
  color: #666;
  padding: 10px;
  background: #f8f9fa;
  border-radius: 8px;
}

.feature-icon {
  font-size: 20px;
}

.back-home-btn {
  background: #2196F3;
  color: white;
  border: none;
  padding: 12px 30px;
  border-radius: 25px;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.back-home-btn:hover {
  background: #1976D2;
  transform: translateY(-2px);
}

/* 内容区域 */
.content-area {
  padding: 20px;
}

.loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
  color: #666;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid #f3f3f3;
  border-top: 3px solid #2196F3;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 15px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
  color: #666;
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 15px;
}

/* 详情内容样式 */
.detail-content {
  background: white;
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 20px;
}

.article-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 20px;
}

.article-title {
  font-size: 20px;
  font-weight: bold;
  color: #333;
  margin: 0;
  flex: 1;
  line-height: 1.4;
}

.read-count {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  color: #666;
  flex-shrink: 0;
}

.eye-icon {
  font-size: 14px;
}

/* 医生信息 */
.doctor-info {
  display: flex;
  align-items: center;
  gap: 15px;
  padding: 15px;
  background: #f8f9fa;
  border-radius: 8px;
  margin-bottom: 20px;
}

.doctor-avatar {
  flex-shrink: 0;
}

.avatar-placeholder {
  width: 50px;
  height: 50px;
  background: #e3f2fd;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
}

.doctor-details {
  flex: 1;
}

.doctor-name {
  font-size: 16px;
  font-weight: bold;
  color: #333;
  margin-bottom: 5px;
}

.doctor-title {
  font-size: 14px;
  color: #666;
  margin-bottom: 3px;
}

.doctor-department {
  font-size: 12px;
  color: #999;
}

.follow-btn {
  background: #2196F3;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 20px;
  font-size: 12px;
  cursor: pointer;
  flex-shrink: 0;
  transition: all 0.3s ease;
}

.follow-btn:hover {
  background: #1976D2;
  transform: translateY(-1px);
}

/* 文章内容 */
.article-content {
  margin-bottom: 20px;
}

.content-text {
  font-size: 16px;
  line-height: 1.6;
  color: #333;
  margin-bottom: 20px;
}

.content-section {
  margin-bottom: 15px;
}

.content-section h3 {
  font-size: 16px;
  font-weight: bold;
  color: #333;
  margin-bottom: 8px;
}

.content-section p {
  font-size: 14px;
  line-height: 1.6;
  color: #666;
}

/* 医生回复 */
.doctor-response {
  background: #f8f9fa;
  border-radius: 8px;
  padding: 15px;
  margin-bottom: 20px;
}

.response-title {
  font-size: 16px;
  font-weight: bold;
  color: #333;
  margin-bottom: 10px;
}

.response-content p {
  font-size: 14px;
  line-height: 1.6;
  color: #333;
  margin-bottom: 10px;
}

.medical-knowledge {
  background: white;
  border-radius: 6px;
  padding: 10px;
  margin-top: 10px;
}

.medical-knowledge strong {
  font-size: 14px;
  color: #333;
}

.medical-knowledge p {
  font-size: 14px;
  color: #666;
  margin-top: 5px;
}

/* 文章底部 */
.article-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 15px;
  border-top: 1px solid #eee;
}

.article-tags {
  display: flex;
  gap: 10px;
}

.tag {
  background: rgba(33, 150, 243, 0.1);
  color: #2196F3;
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 12px;
}

.publish-time {
  font-size: 12px;
  color: #999;
}

/* 底部操作栏 */
.bottom-actions {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: white;
  padding: 15px 20px;
  border-top: 1px solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.comment-btn {
  background: #f5f5f5;
  color: #999;
  border: none;
  padding: 10px 20px;
  border-radius: 20px;
  font-size: 14px;
  cursor: not-allowed;
}

.action-buttons {
  display: flex;
  gap: 20px;
}

.action-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.action-item:hover {
  transform: translateY(-2px);
}

.action-icon {
  font-size: 20px;
  transition: all 0.3s ease;
}

.action-icon.active {
  color: #e91e63;
  transform: scale(1.1);
}

.action-text {
  font-size: 12px;
  color: #666;
}

/* 自定义Toast样式 */
:deep(.van-toast) {
  background: rgba(0, 0, 0, 0.8);
  color: white;
  border-radius: 8px;
  padding: 12px 20px;
  font-size: 14px;
  font-weight: 500;
}

:deep(.van-toast--middle) {
  top: 50%;
  transform: translateY(-50%);
}
</style> 